<?php

namespace App\Http\Controllers\App;

use App\Models\ShopUserBalanceLog;
use Illuminate\Http\Request;
use App\Http\Controllers\App\BaseController;
use App\Models\Shop;
use App\Models\User;
use App\Models\Queue;
use App\Models\UserServerBalance;
class ShopController extends BaseController
{
    
    /**
     * 商户列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shopList(Request $request) 
    {
        
        $params = [];
        $request->input('shopname') == null || $shopname  = $request->input('shopname');
        $request->input('shopuser') == null || $shopuser  = $request->input('shopuser');
        $request->input('login')    == null || $login     = $request->input('login');
        $request->input('status')   == null || $status    = $request->input('status');
    
        $shop = new Shop();

        if (isset($shopname)) {
            $shop = $shop->where('shopname', 'like', "%".$shopname."%");
        }

        if (isset($shopuser)) {
            $shop = $shop->where('shopuser', 'like', "%".$shopuser."%");
        }

        if (isset($login)) {
            $shop = $shop->where('login', 'like', "%".$login."%");
        }

        if (isset($status)) {
            $shop = $shop->where('status', $status);
        }

        $data = $shop->orderBy('displayorder','asc')->paginate($this->pageSize);

        return self::json($data);
    }

    /**
     * 获得单个店铺详情
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shopOne (Request $request) 
    {
        
        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $result = Shop::find($id);

        return empty($result) ? self::error('id有误') : self::json($result);
    }

    /**
     * 店铺添加
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shopAdd(Request $request) 
    {

        $params = [];

        $request->input('shopname')    == null || $params['shopname']     = $request->input('shopname');//店铺名字
        $request->input('shopuser')    == null || $params['shopuser']     = $request->input('shopuser');//店铺所有人
        $request->input('password')    == null || $params['password']     = $request->input('password');//登录密码
        $request->input('avatar')      == null || $params['avatar']       = $request->input('avatar');// 图片/头像等
        $request->input('phone')       == null || $params['phone']        = $request->input('phone');// 店铺电话
        $request->input('tel')         == null || $params['tel']          = $request->input('tel');// 所有人电话
        $request->input('card')        == null || $params['card']         = $request->input('card');//所有人身份证
        $request->input('place')       == null || $params['place']        = $request->input('place');//所有人籍贯
        $request->input('province')    == null || $params['province']     = $request->input('province');//省
        $request->input('city')        == null || $params['city']         = $request->input('city');//市
        $request->input('area')        == null || $params['area']         = $request->input('area');//区
        $request->input('address')     == null || $params['address']      = $request->input('address');//详细地址
        $request->input('latitude')    == null || $params['latitude']     = $request->input('latitude');//纬度
        $request->input('longitude')   == null || $params['longitude']    = $request->input('longitude');//经度
        $request->input('bank')        == null || $params['bank']         = $request->input('bank');//银行
        $request->input('branch')      == null || $params['branch']       = $request->input('branch');//支行
        $request->input('bank_card')   == null || $params['bank_card']    = $request->input('bank_card');//银行卡卡号
        $request->input('wx')          == null || $params['wx']           = $request->input('wx');//微信
        $request->input('qq')          == null || $params['qq']           = $request->input('qq');//qq

        if (count($params) != 19) return self::error('必填参数不能为空');

        $request->input('status')       == null || $params['status']       = $request->input('status');//状态
        $request->input('displayorder') == null || $params['displayorder'] = $request->input('displayorder');//排序
        $request->input('email')        == null || $params['email']        = $request->input('email');//邮箱

        //验证身份证 有效性
        $checkCard = User::idcard_checksum18($params['card']);

        if ($checkCard == false) return self::error('身份证号码错误');

        //生成密码
        $params['password'] = User::create_pwd($params['password']);

        $shopObj = new Shop();

        $params['login'] = $shopObj->getLogin();

        $shopObj->setAttributes($params);

        $result = $shopObj->save();
/*        dd($result,isset($result->id));*/
        //初始化队列信息
        $queue = new Queue();
        $queue->shop_id  = $shopObj->id;
        $queue->desc     = ' ';
        $queue->protocol = ' ';
        $queue->close_reason = '';
        $queue->save();

        return $result ? self::success('添加成功') : self::error('添加失败');
    }

    /**
     * 店铺编辑
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shopEdit(Request $request) 
    {

        $id = intval($request->input('id'));

        if ($id == 0) return self::error('id有误');

        $params = [];

        $request->input('shopname')     == null || $params['shopname']     = $request->input('shopname');//店铺名字
        $request->input('shopuser')     == null || $params['shopuser']     = $request->input('shopuser');//店铺所有人
        $request->input('password')     == null || $params['password']     = $request->input('password');//登录密码
        $request->input('avatar')       == null || $params['avatar']       = $request->input('avatar');// 图片/头像等
        $request->input('phone')        == null || $params['phone']        = $request->input('phone');// 店铺电话
        $request->input('tel')          == null || $params['tel']          = $request->input('tel');// 所有人电话
        $request->input('card')         == null || $params['card']         = $request->input('card');//所有人身份证
        $request->input('place')        == null || $params['place']        = $request->input('place');//所有人籍贯
        $request->input('province')     == null || $params['province']     = $request->input('province');//省
        $request->input('city')         == null || $params['city']         = $request->input('city');//市
        $request->input('area')         == null || $params['area']         = $request->input('area');//区
        $request->input('address')      == null || $params['address']      = $request->input('address');//详细地址
        $request->input('latitude')     == null || $params['latitude']     = $request->input('latitude');//纬度
        $request->input('longitude')    == null || $params['longitude']    = $request->input('longitude');//经度
        $request->input('wx')           == null || $params['wx']           = $request->input('wx');//微信
        $request->input('qq')           == null || $params['qq']           = $request->input('qq');//qq
        $request->input('status')       == null || $params['status']       = $request->input('status');//状态
        $request->input('bank')         == null || $params['bank']         = $request->input('bank');//银行
        $request->input('branch')       == null || $params['branch']       = $request->input('branch');//支行
        $request->input('bankCard')     == null || $params['bank_card']    = $request->input('bankCard');//银行卡卡号
        $request->input('displayorder') == null || $params['displayorder'] = $request->input('displayorder');//排序
        $request->input('email')        == null || $params['email']        = $request->input('email');//邮箱

        if (empty($params)) return self::success('成功');

        if (isset($params['card'])) {
            //验证身份证 有效性
            $checkCard = User::idcard_checksum18($params['card']);

            if ($checkCard == false) return self::error('身份证号码错误');
        }
        
        //生成密码
        if (isset($params['password'])) $params['password'] = User::create_pwd($params['password']);

        $shopObj = Shop::find($id);

        if(empty($shopObj)) return self::error('id有误');

        $shopObj->setAttributes($params);

        $result = $shopObj->save();

        return $result ? self::success('编辑成功') : self::error('编辑失败');
    }

    /**
     * 店铺删除
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shopDelete(Request $request) 
    {

        $id = intval($request->input('id'));

        $shopObj = Shop::find($id);

        if(empty($shopObj)) return self::error('id有误');

        $shopObj->isdelete = Shop::IS_DELETE;

        $result = $shopObj->save();

        return $result ? self::success('删除成功') : self::error('删除失败'); 
    }

    /**
     * 查看会员店铺流水
     */
    public function balanceLog(Request $request)
    {
        $return      = []; //存放取出的数据
        $newArr      = []; //存放处理后的数据
        $shop_id     = $request->input('shop_id','');   //店铺ID
        $card_name   = $request->input('card_name',''); //用户名
        $user_mobile = $request->input('user_mobile',''); //手机号
        $order_sn    = $request->input('order_sn','');    //订单号
        $action_type = $request->input('action_type',''); //进出账选择
        $s_time      = $request->input('s_time');//开始时间
        $e_time      = $request->input('e_time');//结束时间

        //必填参数判断
        if (empty($shop_id)) {

            return self::error('店铺ID不能为空');
        }

        $obj = new ShopUserBalanceLog();

        $obj = $obj->where('shop_id',$shop_id);

        //筛选条件判断
        if (!empty($order_sn)) {

            $obj = $obj->where('order_sn',$order_sn);
        }
        //用户名
        if (!empty($card_name)) {

            $obj = $obj->where('card_name',$card_name);
        }
        //手机号
        if (!empty($user_mobile)) {

            $obj = $obj->where('user_mobile',$user_mobile);
        }

        if (!empty($action_type)) {

            $obj = $obj->where('action_type',$action_type);
        }

        if($s_time > 0){

            $obj = $obj->where('created_at','>=',$s_time);
        }

        if($e_time > 0){

            $obj = $obj->where('created_at','<=',$e_time);
        }

        $data   = $obj->orderBy('created_at','desc')->paginate($this->pagesize)->toArray();

        $return = $data;

        if (empty($return['data'])) {
            return self::json($return,'获取数据为空');
        }

        $objB   = new UserServerBalance();
        $newArr = $objB->translate($return);

        return self::json($return,'成功');
    }
}
